<template>
  <div>
    <el-dialog v-model="state.showDialog" destroy-on-close :title="title" draggable :close-on-click-modal="false"
      :close-on-press-escape="false" width="569px">
      <el-form ref="formRef" :model="form" size="default" label-width="80px">
        <el-row :gutter="35">
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="复制配方" :rules="[{ required: true, message: '请选中配方', trigger: ['blur', 'change'] }]">
              <el-select v-model="form.copyDeviceFormulaId" placeholder="请选中配方">
                <el-option v-for="item in state.deviceFormulaTreeData" :key="item.id" :label="item.formulaOrder"
                  :value="item.id">
                  <span style="float: left">{{ item.formulaOrder }}</span>
                  <span style="
                              float: right;
                              color: var(--el-text-color-secondary);
                              font-size: 13px;">
                    {{ item.formulaName }}
                  </span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="配方代号" prop="formulaCode"
              :rules="[{ required: true, message: '请输入配方代号', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.formulaCode" placeholder="请输入配方代号" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="配方名称" prop="formulaName"
              :rules="[{ required: true, message: '请输入配方名称', trigger: ['blur', 'change'] }]">
              <el-input v-model="form.formulaName" placeholder="请输入配方代号" autocomplete="off" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="配方序号" prop="formulaOrder"
              :rules="[{ required: false, message: '排序', trigger: ['blur', 'change'] }]">
              <el-input-number v-model="form.formulaOrder" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="详情复制" prop="sort"
              :rules="[{ required: false, message: '排序', trigger: ['blur', 'change'] }]">
              <el-switch v-model="form.copyFormulaDetail" inline-prompt active-text="是" inactive-text="否" />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">
            <el-form-item label="排序" prop="sort"
              :rules="[{ required: false, message: '排序', trigger: ['blur', 'change'] }]">
              <el-input-number v-model="form.sort" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel" size="default">取 消</el-button>
          <el-button type="primary" @click="onSure" size="default" :loading="state.sureLoading">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script setup lang="ts" name="industry/device-formula/form">
import { reactive, toRefs, getCurrentInstance, ref } from 'vue'
import { DeviceFormulaCopyInput, DeviceFormulaGetListOutput } from '/@/api/industry/data-contracts'
import { DeviceFormulaApi } from '/@/api/industry/DeviceFormula'
import eventBus from '/@/utils/mitt'
import { listToTree } from '/@/utils/tree'

defineProps({
  title: {
    type: String,
    default: '',
  },
})
const { proxy } = getCurrentInstance() as any
const formRef = ref()
const state = reactive({
  showDialog: false,
  sureLoading: false,
  form: {} as DeviceFormulaCopyInput,
  deviceFormulaTreeData: [] as DeviceFormulaGetListOutput[],
})
const { form } = toRefs(state)

const getDeviceFormulas = async () => {
  const res = await new DeviceFormulaApi().getList({})
  if (res?.success && res.data && res.data.length > 0) {
    state.deviceFormulaTreeData = listToTree(res.data) as DeviceFormulaGetListOutput[]
  } else {
    state.deviceFormulaTreeData = []
  }
}

// 打开对话框
const open = async () => {
  await getDeviceFormulas()
  state.form = {} as DeviceFormulaCopyInput
  state.showDialog = true
}
// 确定
const onSure = () => {
  formRef.value.validate(async (valid: boolean) => {
    if (!valid) return
    state.sureLoading = true
    const res = await new DeviceFormulaApi().copyDeviceFormula(state.form, { showSuccessMessage: true }).catch(() => {
      state.sureLoading = false
    })
    if (res?.success) {
      eventBus.emit('refreshDeviceFormula')
      state.showDialog = false
      state.sureLoading = false
    }
  })
}

// 取消
const onCancel = () => {
  state.showDialog = false
}
defineExpose({
  open,
})
</script>